package wangjg.sso.util;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieUtil {
	public static String getCookieValue(HttpServletRequest request,String name){
		Cookie cookie = findCookie(request, name);
		if(cookie!=null){
			return cookie.getValue();
		}else{
			return null;
		}
	}
	
	public static Cookie findCookie(HttpServletRequest request,String name){
		Cookie[] cookies = request.getCookies();
		return findCookie(cookies,name);
	}
	
	public static Cookie findCookie(Cookie[] cookies,String name){
		if(cookies==null){
			return null;
		}
		for(int i=0;i<cookies.length;i++){
			if(cookies[i].getName().equals(name)){
				return cookies[i];
			}
		}
		return null;
	}

	public static void setCookie(HttpServletResponse response,Cookie cookie){
		response.addCookie(cookie);
	}
	
	//in header
	public static Cookie findCookie(HttpServletResponse response,String name){
		
		return null;
	}
	
	public static void setCookieHeader(HttpServletResponse response,Cookie cookie){
		String str="";
		response.addHeader("Set-Cookie", str);
	}
	
}
